﻿@model QLTL_V3.Models.Order
@{
    ViewBag.Title = "Create";
}
<script src="../../Scripts/jquery.formatCurrency-1.4.0.js" type="text/javascript"></script>
<link href="../../Content/css/smoothness/jquery-ui-1.8.16.custom.css" rel="stylesheet" type="text/css" />
<h2>
    TAO HOA DON MOI</h2>
<script type="text/javascript">
    $(function () {
        $("#CustomerTerm").autocomplete({
            source: function (request, response) {
                // define a function to call your Action (assuming UserController)
                $.ajax({
                    url: '/Customer/SearchCustomer', type: "POST", dataType: "json",
                    // query will be the param used by your action method
                    data: { term: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Name + "----" + item.Address, value: item.Name, Name: item.Name,
                                Address: item.Address, PhoneNo: item.PhoneNo, CustomerId: item.CustomerId,
                                CustomerTypeName: item.CustomerTypeName, CustomerTypeId: item.CustomerTypeId
                            }
                        }))
                    }
                })
            },
            select: function (event, ui) {
                document.getElementById("CustomerName").value = ui.item.Name;
                document.getElementById("CustomerAddress").value = ui.item.Address;
                document.getElementById("CustomerPhoneNo").value = ui.item.PhoneNo;
                document.getElementById("CustomerId").value = ui.item.CustomerId;
                document.getElementById("CustomerTypeName").value = ui.item.CustomerTypeName;
                document.getElementById("CustomerTypeId").value = ui.item.CustomerTypeId;


            },

            change: function (event, ui) {
                if (ui.item == null) {
                    document.getElementById("CustomerName").value = ''
                    document.getElementById("CustomerAddress").value = ''
                    document.getElementById("CustomerPhoneNo").value = ''
                    document.getElementById("CustomerId").value = '';
                    document.getElementById("CustomerTerm").focus();
                    document.getElementById("CustomerTypeName").value = '';
                    document.getElementById("CustomerTypeId").value = '';
                }
            },


            minLength: 1 // require at least one character from the user
        });

    });


    $(document).keydown(function (e) {
        if (e.which == 18) isAlt = true;
        if (e.which == 83 && isAlt == true) {
            //run code for CTRL+S -- ie, save!
            document.getElementById("CustomerTerm").focus();
            isAlt = false;
            return false;
        } else if (e.which == 67 && isAlt == true) // Ctr - C to Customer Search
        {
            document.getElementById("CustomerTerm").focus();
            document.getElementById("CustomerTerm").select();
            isAlt = false;
            return false;

        } else if (e.which == 80 && isAlt == true) // Ctr - P to Customer Search
        {
            document.getElementById("Price").focus();
            document.getElementById("Price").select();
            isAlt = false;
            return false;
        }
    });


    function UpdatePrice() {
        var CustomerTypeId = document.getElementById("CustomerTypeId").value;
        var ProductId = document.getElementById("ProductId").value;
        var ProductUnitId = document.getElementById("ProductUnitId").value;
        $.post("/BuyPrice/GetPrice", { ProductId: ProductId, ProductUnitId: ProductUnitId, CustomerTypeId: CustomerTypeId },
                    function (data) {
                        // Successful requests get here
                        // Update the page elements
                        document.getElementById("Price").value = data.Price;
                        UpdateTotalAmount();
                    });


    }


    function UpdateTotalAmount() {
        var amount = document.getElementById("Amount").value;
        var price = document.getElementById("Price").value;
        var n_price = Number(price.replace(/[^0-9\.]+/g, ""));
        document.getElementById("TotalAmount").value = amount * n_price;
        $('.currency').formatCurrency();
    };


    $(document).ready(function () {
        //  o_Price = document.getElementById("Price");
        // o_Price.onchange = function () { UpdateTotalAmount(); };
        //  o_Price.setAttribute("class", "currency");

    });
  
    
</script>
<div class="editor-label">
    Tìm Khách Hàng Theo Tên
</div>
@Html.TextBox("CustomerTerm")
<fieldset>
    <legend>Thông Tin Khách Hàng</legend>
    <div class="editor-label">
        Tên Khách Hàng
    </div>
    <div class="editor-field">
        @Html.TextBox("CustomerName", null, new { disabled = "true" })
    </div>
    <div class="editor-label">
        Địa Chỉ
    </div>
    <div class="editor-field">
        @Html.TextBox("CustomerAddress", null, new { disabled = "true" })
    </div>
    <div class="editor-label">
        Số Điện Thoại
    </div>
    <div class="editor-field">
        @Html.TextBox("CustomerPhoneNo", null, new { disabled = "true" })
    </div>
    <div class="editor-label">
        Loại Khách Hàng
    </div>
    <div class="editor-field">
        @Html.TextBox("CustomerTypeName", null, new { disabled = "true" })
    </div>
    <div class="editor-field">
        @Html.Hidden("CustomerId")
    </div>
    <div class="editor-field">
        @Html.Hidden("CustomerTypeId")
    </div>
</fieldset>
@using (Ajax.BeginForm("AddItem", new AjaxOptions
{
    HttpMethod = "GET",
    UpdateTargetId = "ItemDetails"
}))
{
    <table>
        <tr>
            <th>
                Sản Phẩm
            </th>
            <th>
                Đơn Vị
            </th>
            <th>
                Đơn Giá
            </th>
            <th>
                Số Luợng
            </th>
            <th>
                Thành Tiền
            </th>
        </tr>
        <tr>
            <td>
                @Html.DropDownList("ProductId", (SelectList)ViewBag.ProductId, new { @OnChange = "UpdatePrice()" })
            </td>
            <td>
                @Html.DropDownList("ProductUnitId", null, new { @OnChange = "UpdatePrice()" })
            </td>
            <td>
                @Html.TextBox("Price", null, new { @OnChange = "UpdateTotalAmount()", @class = "currency" })
            </td>
            <td>
                @Html.TextBox("Amount", 1, new { @OnChange = "UpdateTotalAmount()" })
            </td>
            <td>
                @Html.TextBox("TotalAmount", null, new { @OnChange = "UpdateTotalAmount()", @class = "currency", @disabled = "true" })
                @Html.Hidden("OrderId", @Model.OrderId)
                <input type="submit" value="Thêm" />
            </td>
        </tr>
    </table>
}
<div id="ItemDetails">
    @{        
        if (Model.OrderDetails != null)
        {
            var test = Model.OrderDetails;
            Html.RenderPartial("_OrderDetailPartial", test.ToList());
        }
      
    }
</div>
